package edu.fasta.sisinfo2.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import edu.fasta.sisinfo2.net.DatosUsuario;
import edu.fasta.sisinfo2.net.InterfazComandoServer;
import edu.fasta.sisinfo2.net.LoginVerify;
import edu.fasta.sisinfo2.poker.PokerControllerCliente;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.io.IOException;

public class SignIn extends JDialog {

	private final JPanel contentPanel = new JPanelFondo("/Img/Login2.png",0,0);
	private String nombreUsuario;
	private String pswUsuario;
	private JTextField textField;
	private JPasswordField passwordField;
	private PokerControllerCliente PCC;
	private InterfazComandoServer comando;
	private Icon i1 = new ImageIcon(getClass().getResource( "/Img/Ingresar1.png" ));
	private Icon i2 = new ImageIcon(getClass().getResource( "/Img/Ingresar2.png" ));
	private Icon i3 = new ImageIcon(getClass().getResource( "/Img/Ingresar3.png" ));
	private Icon s1 = new ImageIcon(getClass().getResource( "/Img/Salir1.png" ));
	private Icon s2 = new ImageIcon(getClass().getResource( "/Img/Salir2.png" ));
	private Icon s3 = new ImageIcon(getClass().getResource( "/Img/Salir3.png" ));
	private Icon r1 = new ImageIcon(getClass().getResource( "/Img/Reg4.png" ));
	private Icon r2 = new ImageIcon(getClass().getResource( "/Img/Reg5.png" ));
	private Icon r3 = new ImageIcon(getClass().getResource( "/Img/Reg3.png" ));

	/**
	 * Create the dialog.
	 */
	public SignIn(PokerControllerCliente unPCC) {
		this.setPCC(unPCC);
		this.setResizable(false);
		setTitle("Texas UFasta - Ingreso");
		setBounds(100, 100, 321, 218);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		
		JLabel lblUsername = new JLabel("Usuario");
		lblUsername.setBounds(59, 41, 62, 20);
		contentPanel.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Clave");
		lblPassword.setBounds(65, 73, 51, 20);
		contentPanel.add(lblPassword);
		
		textField = new JTextField();
		textField.setBounds(125, 41, 173, 20);
		contentPanel.add(textField);
		textField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(125, 73, 173, 20);
		contentPanel.add(passwordField);
		
		JLabel lblIngresarDatosDe = new JLabel("Ingresar datos de su Cuenta de Usuario");
		lblIngresarDatosDe.setForeground(Color.WHITE);
		lblIngresarDatosDe.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblIngresarDatosDe.setBounds(43, 11, 248, 20);
		contentPanel.add(lblIngresarDatosDe);
		
		JButton btnGenerarNuevoUsuario = new JButton( "", r1 );
		btnGenerarNuevoUsuario.setRolloverIcon( r2 );
		btnGenerarNuevoUsuario.setPressedIcon(r3);
		btnGenerarNuevoUsuario.setBorder(BorderFactory.createEmptyBorder());
		btnGenerarNuevoUsuario.setContentAreaFilled(false);
		
		btnGenerarNuevoUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JDialog dialogo = new SignUp(getPCC());
				dialogo.setModal(true);
				dialogo.setVisible(true);
			}
		});
		btnGenerarNuevoUsuario.setBounds(43, 151, 248, 23);
		contentPanel.add(btnGenerarNuevoUsuario);
		{
			
			JButton okButton = new JButton( "", i1 );
			okButton.setRolloverIcon( i2 );
			okButton.setPressedIcon(i3);
			okButton.setBorder(BorderFactory.createEmptyBorder());
			okButton.setContentAreaFilled(false);
			okButton.setBounds(44, 105, 114, 39);
			contentPanel.add(okButton);
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					setNombreUsuario(textField.getText());
					setPswUsuario(passwordField.getText());
					try{
						getPCC().enviarComando(new DatosUsuario(getNombreUsuario(), getPswUsuario(),1));
						setComando(getPCC().recibirComando());
						switch ((((LoginVerify)comando).getTipomensaje())){
						case 1:
							comando.ejecutarComando(getPCC().getPantalla());
							dispose();
							break;							
						case 2:
							((LoginVerify)comando).ejecutarComandoError(getPCC().getPantalla());
							//resetear conexion
							getPCC().getCliente().cerrarConexion();
							getPCC().getCliente().iniciar();
							break;
						case 3:
							((LoginVerify)comando).ejecutarComandoEsperar(getPCC().getPantalla());
							dispose();
							break;	
						case 4:
							((LoginVerify)comando).ejecutarComandoSinLugar(getPCC().getPantalla());
							dispose();
							break;	
						}//fin switch case
					}catch (Exception e){
						System.out.println("\n error en la conexion! - Time Out ");
						JOptionPane.showMessageDialog(null,"error en la conexion! - Time Out");
						System.exit(0);
					}finally{}
				}
			});
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			JButton cancelButton = new JButton( "", s1 );
			cancelButton.setRolloverIcon( s2 );
			cancelButton.setPressedIcon(s3);
			cancelButton.setBorder(BorderFactory.createEmptyBorder());
			cancelButton.setContentAreaFilled(false);
			cancelButton.setBounds(176, 105, 114, 39);
			contentPanel.add(cancelButton);
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					//getPCC().finalizarPCC();
					System.exit(0);
				}
			});
			cancelButton.setActionCommand("Salir");
		}
		this.setLocationRelativeTo(null);
	}


	public String getNombreUsuario() {
		return nombreUsuario;
	}

	public void setNombreUsuario(String nombreUsuario) {
		this.nombreUsuario = nombreUsuario;
	}

	public String getPswUsuario() {
		return pswUsuario;
	}

	public void setPswUsuario(String pswUsuario) {
		this.pswUsuario = pswUsuario;
	}

	public PokerControllerCliente getPCC() {
		return PCC;
	}

	public void setPCC(PokerControllerCliente pCC) {
		PCC = pCC;
	}

	public InterfazComandoServer getComando() {
		return comando;
	}

	public void setComando(InterfazComandoServer comando) {
		this.comando = comando;
	}
}
